package com.accloud.cloudservice;

import com.accloud.service.ACException;
import com.accloud.utils.LocalUtils;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketAddress;

/* access modifiers changed from: package-private */
public class BroadcastTask {
    private boolean cancel;
    private final int interval;
    private LocalMessage localMessage;
    private final int timeout;

    public BroadcastTask(int timeout2, int interval2, LocalMessage localMessage2) {
        this.interval = interval2;
        this.timeout = timeout2;
        this.localMessage = localMessage2;
    }

    public void execute(final BaseCallback callback) {
        new Thread() {
            /* class com.accloud.cloudservice.BroadcastTask.AnonymousClass1 */

            @Override // java.lang.Thread, java.lang.Runnable
            public void run() {
                int time = 0;
                try {
                    DatagramPacket packet = LocalUtils.formatBroadcast(BroadcastTask.this.localMessage.toBytes(), 7689);
                    DatagramSocket socket = new DatagramSocket((SocketAddress) null);
                    do {
                        socket.send(packet);
                        Thread.sleep((long) BroadcastTask.this.interval);
                        time += BroadcastTask.this.interval;
                        if (time >= BroadcastTask.this.timeout) {
                            return;
                        }
                    } while (!BroadcastTask.this.cancel);
                } catch (IOException | InterruptedException e) {
                    callback.error(new ACException(ACException.INTERNAL_ERROR, e));
                }
            }
        }.start();
    }

    public void cancel() {
        this.cancel = true;
    }
}
